package com.bs.action;

import java.util.List;

import net.sf.json.JSONArray;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;

import com.bs.bean.Unit;
import com.bs.service.UnitService;

@SuppressWarnings("serial")
@ParentPackage("struts-default")
@Namespace("/unit")
public class UnitAction extends ActionBase {

	private Unit unit;
	private UnitService unitService;
	public Unit getUnit() {
		return unit;
	}
	public void setUnit(Unit unit) {
		this.unit = unit;
	}
	public UnitService getUnitService() {
		return unitService;
	}
	public void setUnitService(UnitService unitService) {
		this.unitService = unitService;
	}
	
	@Action("save")
	public void save() throws Exception{
		try {
			this.unitService.save(unit);
			response.getWriter().write(this.successInfo("添加货品单位成功"));
		} catch (Exception e) {
			response.getWriter().write(this.errorInfo("添加货品单位失败，可能单位名称已存在"));
			e.printStackTrace();
		}
	}
	@Action("delete")
	public void batchDelete() throws Exception{
		try {
			String ids = (String) request.getParameter("ids");
			int rows = this.unitService.batchDelete(ids);
			if(rows >= 0){
				response.getWriter().write(this.successInfo("删除货品单位成功"));
			}
		} catch (Exception e) {
			response.getWriter().write(this.errorInfo("货品单位正在被使用，不可删除"));
			e.printStackTrace();
		}
	}
	
	@Action("query")
	public void query() throws Exception{
		try {
			List<Unit> list = this.unitService.query(unit);
			response.getWriter().write(JSONArray.fromObject(list).toString());
		} catch (Exception e) {
			response.getWriter().write(this.sysError());
			e.printStackTrace();
		}
	}
	

}